package com.neuedu.javase.jdbc.dao;

import com.neuedu.javase.jdbc.entity.Role;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-12 11:20
 * 描述 :
 */
public class RoleDaoTest {
//    RoleDao dao = new RoleDao();

    RoleDao dao = null;
    @Before  //在执行@Test的单元测试方法之前 初始化一些数据
    public void setUp() throws Exception {
        dao = new RoleDao();
    }

    @Test
    public void selectById() {
//        RoleDao dao = new RoleDao();

        int id = 1;
        Role role = dao.selectById(id);

        System.out.println("role = " + role);


    }

    @Test
    public void selectList() {
        String condation = " where createtime > ? ";
        Object[] params = {new Date(2022-1900,9,12,11,2,13)};
        List<Role> roles = dao.selectList(condation,params);


        roles.forEach(System.out::println); //1.8

    }

    @Test
    public void selectPageList() {
        String condation = " where createtime > ? ";
        Object[] params = {new Date(2022-1900,9,12,11,2,13)};
        int current = 1;
        int size = 2;
        List<Role> roles = dao.selectList(condation,current ,size , params);
        System.out.println("======每页显示"+size+"条，当前页号："+current);
        roles.forEach(System.out::println); 


        current = 2;
//        size = 2;
       roles = dao.selectList(condation,current ,size , params);
        System.out.println("=======每页显示"+size+"条，当前页号："+current);
        roles.forEach(System.out::println);

    }



    @Test
    public void selectPageTotal() {
        String condation = " where createtime > ? ";
        Object[] params = {new Date(2022-1900,9,12,11,2,13)};
        int current = 1;
        int size = 2;
        List<Role> roles = dao.selectList(condation,current ,size , params);
        long count = dao.selectCount(condation, params);
        System.out.println("======每页显示"+size+"条，当前页号："+current+"总条数: "+count);
        roles.forEach(System.out::println);


        current = 2;
        //        size = 2;
        roles = dao.selectList(condation,current ,size , params);
        System.out.println("=======每页显示"+size+"条，当前页号："+current+"总条数: "+count);
        roles.forEach(System.out::println);

    }

    @Test
    public void insert() {

        String sql = "insert  into role(role_name) values(?)";
        String roleName = "测试角色";
        int count = dao.insert(sql, roleName);

        System.out.println("count = " + count);
        //断言
        Assert.assertEquals("插入失败",1,count);


    }


    @Test
    public void update() {

        int id = 5;
        Role role = dao.selectById(id); //到页面展示
        System.out.println("修改前： role = " + role);
        //获取用户修改的项目
        String roleName = "update角色";
        String updateSql = "update role set role_name = ? where role_id = ?";

//        int updateCount = dao.update(updateSql, new Object[]{roleName, id});
        int updateCount = dao.update(updateSql, roleName, id);


        System.out.println("updateCount = " + updateCount);
        //断言
        Assert.assertEquals("更新成功",1,updateCount);


    }


    @Test
    public void deleteById() {

        int id = 5;
        int count = dao.deleteById(id);
        System.out.println("count = " + count);
        Assert.assertEquals(1,count);
    }
}











